package cn.iocoder.yudao.module.system.dal.mysql.subjectscoredetail;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.subjectscoredetail.SubjectScoreDetailDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 科目成绩详情 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface SubjectScoreDetailMapper extends BaseMapperX<SubjectScoreDetailDO> {

    default PageResult<SubjectScoreDetailDO> selectPage(PageParam pageParam, Long examId, Long studentId, String subjectName) {
        return selectPage(pageParam, new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .eqIfPresent(SubjectScoreDetailDO::getExamId, examId)
                .eqIfPresent(SubjectScoreDetailDO::getStudentId, studentId)
                .likeIfPresent(SubjectScoreDetailDO::getSubjectName, subjectName)
                .orderByDesc(SubjectScoreDetailDO::getId));
    }

    default List<SubjectScoreDetailDO> selectList(Long examId, Long studentId, String subjectName) {
        return selectList(new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .eqIfPresent(SubjectScoreDetailDO::getExamId, examId)
                .eqIfPresent(SubjectScoreDetailDO::getStudentId, studentId)
                .likeIfPresent(SubjectScoreDetailDO::getSubjectName, subjectName)
                .orderByDesc(SubjectScoreDetailDO::getId));
    }

    default List<SubjectScoreDetailDO> selectListByExamId(Long examId) {
        return selectList(SubjectScoreDetailDO::getExamId, examId);
    }

    default List<SubjectScoreDetailDO> selectListByStudentId(Long studentId) {
        return selectList(SubjectScoreDetailDO::getStudentId, studentId);
    }

    default SubjectScoreDetailDO selectByExamIdAndStudentIdAndSubject(Long examId, Long studentId, String subjectName) {
        return selectOne(new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .eq(SubjectScoreDetailDO::getExamId, examId)
                .eq(SubjectScoreDetailDO::getStudentId, studentId)
                .eq(SubjectScoreDetailDO::getSubjectName, subjectName));
    }

    default List<SubjectScoreDetailDO> selectElectiveSubjects(Long examId, Long studentId) {
        return selectList(new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .eq(SubjectScoreDetailDO::getExamId, examId)
                .eq(SubjectScoreDetailDO::getStudentId, studentId)
                .eq(SubjectScoreDetailDO::getIsElectiveSubject, 1)
                .orderByDesc(SubjectScoreDetailDO::getOriginalScore));
    }

    default List<SubjectScoreDetailDO> selectByExamIdAndSubject(Long examId, String subjectName) {
        return selectList(new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .eq(SubjectScoreDetailDO::getExamId, examId)
                .eq(SubjectScoreDetailDO::getSubjectName, subjectName)
                .orderByDesc(SubjectScoreDetailDO::getOriginalScore));
    }

    default List<String> selectDistinctSubjectsByExamId(Long examId) {
        return selectObjs(new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .select(SubjectScoreDetailDO::getSubjectName)
                .eq(SubjectScoreDetailDO::getExamId, examId)
                .groupBy(SubjectScoreDetailDO::getSubjectName));
    }

    default List<SubjectScoreDetailDO> selectByExamIdAndStudentId(Long examId, Long studentId) {
        return selectList(new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .eq(SubjectScoreDetailDO::getExamId, examId)
                .eq(SubjectScoreDetailDO::getStudentId, studentId)
                .orderByAsc(SubjectScoreDetailDO::getSubjectName));
    }

    /**
     * 根据考试ID删除所有科目成绩详情（用于清除导入的数据）
     */
    default int deleteByExamId(Long examId) {
        return delete(new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .eq(SubjectScoreDetailDO::getExamId, examId));
    }

    /**
     * 批量删除科目成绩详情（根据考试ID和学生ID列表）
     */
    default int deleteByExamIdAndStudentIds(Long examId, List<Long> studentIds) {
        if (studentIds == null || studentIds.isEmpty()) {
            return 0;
        }
        return delete(new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .eq(SubjectScoreDetailDO::getExamId, examId)
                .in(SubjectScoreDetailDO::getStudentId, studentIds));
    }
    
    /**
     * 根据导入批次ID删除科目成绩详情
     */
    default int deleteByImportBatchId(String importBatchId) {
        return delete(new LambdaQueryWrapperX<SubjectScoreDetailDO>()
                .eq(SubjectScoreDetailDO::getImportBatchId, importBatchId));
    }
    
    /**
     * 根据导入批次ID查询科目成绩详情数量
     */
    default Long selectCountByImportBatchId(String importBatchId) {
        return selectCount(SubjectScoreDetailDO::getImportBatchId, importBatchId);
    }

}